package framework.states;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.UnknownHostException;

import org.newdawn.slick.AngelCodeFont;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.gui.TextField;
import org.newdawn.slick.state.StateBasedGame;

import framework.C;

public class JoinFight extends BackableState {

	int dots = 0;

	int count = 0;
	/** */
	private String addressInput, connectNotification;
	/** Socket of the remote fight */
	private Socket serverSocket;
	/** To gather the state we want to jump in */
	private StateBasedGame game;

	private boolean entered = false;
	
	AngelCodeFont font;
	
	private TextField address;

	@Override
	public void init(GameContainer gc, StateBasedGame game)
			throws SlickException {
		super.init(gc, game);
		this.addressInput = "25.145.207.193";
		this.connectNotification = "";
		this.game = game;
		font = new AngelCodeFont(C.FONTS_FOLDER + "little.fnt", C.FONTS_FOLDER + "little.png");
		address = new TextField(gc, font, 500, 100, 500, 50);
	}
	
	@Override
	public void enter(GameContainer gc, StateBasedGame game)
			throws SlickException {
		back = false;
	}

	@Override
	public void render(GameContainer gc, StateBasedGame game, Graphics g)
			throws SlickException {
		String dot;
		switch(dots) {
		case 1 : dot = ".";
		break;
		case 2 : dot = "..";
		break;
		case 3 : dot = "...";
		break;		
		default : dot = "";
		break;
		}

		g.setFont(font);
		g.drawString("Type the address : ", 100, 100);
		address.render(gc, g);

		if (entered) {
			g.drawString("Connection attempt to the address : " + addressInput + dot, 100, 200);
		}
		g.drawString(connectNotification, 100, 250);
	}

	@Override
	public void update(GameContainer gc, StateBasedGame game, int delta)
			throws SlickException {
		super.update(gc, game, delta);
		count += delta;
		if (count > 250) {
			dots = (dots + 1) % 4;
			count = 0;
		}
	}

	@Override
	public int getID() {
		return 6;
	}

	@Override
	public void keyPressed(int key, char c) {
		super.keyPressed(key, c);
		String addressInput = address.getText();
		if (!entered) {
			if (Input.KEY_ENTER == key) {
				joinFight(addressInput);
			} else if(Input.KEY_BACK == key && addressInput.length() > 0) {
				addressInput = addressInput.substring(0, addressInput.length() - 1);
			} else {
				addressInput += c;
			}
		} else {

		}
	}

	private void joinFight(String host) {
		final String hostName = host;

		(new Thread() {
			@Override
			public void run() {
				try {
					entered = true;
					// contacting the server
					serverSocket = new Socket();
					serverSocket.connect(new InetSocketAddress(hostName, 2346), 10000);

					// receiving the response from the server
					connectNotification = "Found a fight!";
					Thread.sleep(1000);
				} catch (UnknownHostException e) {
					connectNotification = "The host doesn't exists.";
					e.printStackTrace();
				} catch (IOException e) {
					connectNotification = "Error.";
					e.printStackTrace();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				((OnlineFight) game.getState(3)).setActivePlayer(2);
				((OnlineFight) game.getState(3)).setCommunicationSocket(serverSocket);
				game.enterState(3);
				entered = false;
			}
		}).start();	
	}
}

